﻿/*** MikeWare Framework ************************************
* This class is part of product of MikeWare.Framework.
* 
* Author      : Mike Cheers
* Mail        : mikecheers@126.com
* Create Date : 2018/9/14
* Summary     : 
* 
* 
* Modified By : 
* Date        : 
* Mail        : 
* Comment     : 
**************************************************************/

namespace MikeWare.Core
{
    using System;
    using System.Text;

    public partial class Result : IDisposable
    {
        public Exception Exception { get; set; }

        private bool isSuccess = false;

        public bool IsSuccess
        {
            get { return isSuccess; }
            set { isSuccess = value; }
        }

        private object value;

        public object Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        private string message = string.Empty;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        public string ReturnCode { get; set; }

        public void CopyFrom(Result result)
        {
            IsSuccess = result.IsSuccess;
            Value = result.Value;
            Message = result.Message;
            ReturnCode = result.ReturnCode;
        }

        public byte[] ToBytes()
        {
            byte[] isSuccessBuffer = BitConverter.GetBytes(IsSuccess);

            string valuestring = null == Value ? string.Empty : Value.ToString();
            byte[] valuebuffer = Encoding.ASCII.GetBytes(valuestring ?? string.Empty);
            byte[] valuelength = BitConverter.GetBytes(valuebuffer.Length);

            byte[] messagebuffer = new UTF8Encoding(false).GetBytes(Message ?? String.Empty);
            byte[] messagelength = BitConverter.GetBytes(messagebuffer.Length);

            int length = sizeof(Int32)
                + isSuccessBuffer.Length
                + valuelength.Length
                + valuebuffer.Length
                + messagelength.Length
                + messagebuffer.Length;

            byte[] returnmodelbuffer = new byte[length];
            int position = 0;

            BitConverter.GetBytes(length).CopyTo(returnmodelbuffer, position);
            position += sizeof(Int32);

            isSuccessBuffer.CopyTo(returnmodelbuffer, position);
            position += isSuccessBuffer.Length;

            valuelength.CopyTo(returnmodelbuffer, position);
            position += valuelength.Length;

            valuebuffer.CopyTo(returnmodelbuffer, position);
            position += valuebuffer.Length;

            messagelength.CopyTo(returnmodelbuffer, position);
            position += messagelength.Length;

            messagebuffer.CopyTo(returnmodelbuffer, position);
            position += messagebuffer.Length;

            return returnmodelbuffer;
        }

        public void Restore(byte[] buffer, ref int dataposition)
        {
            int length = 0;
            Int32 returnmodelsize = BitConverter.ToInt32(buffer, dataposition);
            dataposition += sizeof(Int32);

            IsSuccess = BitConverter.ToBoolean(buffer, dataposition);
            dataposition += sizeof(bool);

            length = BitConverter.ToInt32(buffer, dataposition);
            dataposition += sizeof(Int32);

            Value = Encoding.ASCII.GetString(buffer, dataposition, length);
            dataposition += length;

            length = BitConverter.ToInt32(buffer, dataposition);
            dataposition += sizeof(Int32);

            Message = Encoding.ASCII.GetString(buffer, dataposition, length);
            dataposition += length;
        }

        public string ToSummary()
        {
            return IsSuccess + ": " + Message;
        }

        public void Dispose()
        {
            IsSuccess = false;
            Value = null;
            Message = null;
        }
    }
}
